#include "MenuSystem.h"
#include "IrControl.h"
#include "Menu/Tool/frame.h"

#define ITEM_SET_AUDIO	(0)
#define ITEM_SET_VIDEO	(1)
#define ITEM_SET_EXIT	(2)


TRect		MenuSetTouchRect[IMG_MENU_SET_ITEM_QTY];
TPosCoor	MenuSetItemPos[IMG_MENU_SET_ITEM_QTY] ={ {28,164}, {252,164}, {576,374} };
int		MenuSetFocus = -1; //{ {26,142}, {273,142}, {520,142}, {272,275} };
char*		MenuSetInfoStr[IMG_MENU_SET_ITEM_QTY] = {"AUDIO","VIDEO","EXIT"};
TFrame		MenuSetFrame;
extern	unsigned char read_info_data[10];
extern unsigned char navi_status;
extern  TRect	  MuteInfoPos;
extern int volume_count ;
extern int volume_temp;
extern unsigned char mute_status;
extern unsigned char	VOLvalue ;
extern struct _VolumePos VolumePos;
extern TRect	BarInfoRect;
extern unsigned char rece_handle;	
extern unsigned char dtpresent_flag;
extern pthread_cond_t dt_cond;
extern 		int 	Last_ID ;


void MenuSetDrawBackgroup(TCanvas *pCanvas)
{
	DrawJpeg(pCanvas, 0, 0, "/res_jpeg/bkg.jpg" );
	DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
	g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "SETTING" );

}

void MenuSetDrawVolume( TCanvas *pCanvas,int flag)
{
	int GetJpegIdx;
	TRect rect;
	TJpeg pJpeg;
	char FileName[20];
	int x,y;
	int i;
	switch (flag)
		{							
			case 1://disp volume all
				GetJpegIdx = IMG_VOLUME_BARCOVER;
				sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);	
				DrawJpeg(pCanvas, VolumePos.Volume_all.x, VolumePos.Volume_all.y, FileName);	
				if (mute_status == 1)
					{
						GetJpegIdx = IMG_VOLUME_MUTE ;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						DrawJpeg(pCanvas, VolumePos.Volume_speaker.x, VolumePos.Volume_speaker.y, FileName);	
					}
				else
						mute_status = 2;
				
				for (i = 0; i<VOLvalue; i++)
					{
					if (i%2)
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) + VolumePos.Barwidth;
						GetJpegIdx = IMG_VOLUME_RIGHTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					else
						{
						x = VolumePos.Volume_bar_start.x+VolumePos.Distance*(i/2) ;
						GetJpegIdx = IMG_VOLUME_LEFTFOCUS;
						sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_OTHER_FOLDER, GetJpegIdx);
						}
					y = VolumePos.Volume_bar_start.y;					
					DrawJpeg(pCanvas,x, y, FileName);						
					}
			break;			
			
			case 2://draw bar
			
			break;
			
			case 3://undisp
				DrawJpeg(pCanvas, 0, 8, "/res_jpeg/bar.jpg" );
				g_Puts( pCanvas, FONT33, BarInfoRect, TEXT_ALIGN_CENTER, "SETTING" );
			break;
		}
	
	pJpeg = GetJpegPtr("/res_jpeg/bar.jpg" );
	rect.left = BARPOS_X;
	rect.right = rect.left + pJpeg.Width;
	rect.top = BARPOS_Y;
	rect.bottom = rect.top + pJpeg.Height;	
	dt_Present_part(rect);	
	volume_count = 0;
	volume_temp = 0;

}


static void VolumeCountHandle(void)
{
	volume_count ++;
		if (volume_count >0xFF00)
			{
				volume_temp ++;
				volume_count = 0;
			}
		if (volume_temp > 10)
			{
				MenuSetDrawVolume(MenuSetFrame.pCanvas,3);					
				volume_count = 0;	
				volume_temp = 0;	
				mute_status =0;
			}
}

void MenuSetDrawItem( TCanvas *pCanvas, int idx, BOOL bfocus )
{
//	TImage *pImg;
	TJpeg pJpeg;
	TPosCoor pos;
	char FileName[20];
	int GetJpegIdx;

	if( idx < 0 || idx >= IMG_MENU_SET_ITEM_QTY )
		return;


	if( bfocus ){
		GetJpegIdx = IMG_MENU_SET_AUDIO_ITEM_FOCUS+idx ;
	}else{
		GetJpegIdx = IMG_MENU_SET_AUDIO_ITEM+idx ;
	}
	pos = MenuSetItemPos[idx];
	g_SetTextFormat( FONT33, TEXT_NORMAL, RGB_WHITE, 0, 0 );
	sprintf( FileName, "res_jpeg/%d/%d.jpg", MENU_SET_FOLDER, GetJpegIdx);
//	pJpeg = GetJpegPtr(FileName);
//	DrawJpegRemBG(pCanvas, pos.x, pos.y, FileName,0,255,255);/////////////hangsheng 20071227 add
	pJpeg = GetJpegPtr(FileName);
	DrawJpeg(pCanvas, pos.x, pos.y, FileName);
	MenuSetTouchRect[idx].left = pos.x;
	MenuSetTouchRect[idx].right = pos.x + pJpeg.Width;
	MenuSetTouchRect[idx].top = pos.y;
	MenuSetTouchRect[idx].bottom = pos.y + pJpeg.Height;
	g_Puts( pCanvas, FONT33, MenuSetTouchRect[idx], TEXT_ALIGN_CENTER, MenuSetInfoStr[idx]);


}

void MenuSetDraw( BOOL bDrawAll )
{
	int i;
	if( bDrawAll )
		MenuSetDrawBackgroup(MenuSetFrame.pCanvas);
	for( i = 0; i < IMG_MENU_SET_ITEM_QTY; i++ )
		MenuSetDrawItem( MenuSetFrame.pCanvas, i, FALSE );
		if (mute_status == 1)
		{
			MenuSetDrawVolume(MenuSetFrame.pCanvas,1);		
		}
	dtpresent_flag++;		
	pthread_cond_signal(&dt_cond);
	//dt_Present();
}

int MenuSetGetTouchID( TPosCoor pos )
{
	int i;
	for( i = 0; i < IMG_MENU_SET_ITEM_QTY; i++ )
		if( ut_PtInRect( pos, MenuSetTouchRect[i] ) )
			return i;
	return -1;
}

BOOL MenuSetBtnEnter(int idx)
{
	printf (" in button handle~~\n");
	switch(idx)
	{
	case ITEM_SET_AUDIO:
		MenuAudioInit();		
		break;
	case ITEM_SET_VIDEO:
		MenuVideoInit();
		break;
	case ITEM_SET_EXIT:
		MenuMainInit();
		break;
	}
	return FALSE;
}

void MenuSetInit()
{

		ir_SetCurrentID( IR_MENU_SET_INIT );		
		frm_Create( &MenuSetFrame, FRM_NO_TITLE|FRM_NO_TASK, "" );
		MenuSetDraw( TRUE );		
}

/*****************************
function:message_dispatch

 return:
  0: successful
  1: need other menu handle
  -1:failure
*****************************/
static int message_dispatch (void)
{
	int rc = 1;
	
		if (read_info_data[0] ==0xe0)
		{
			printf ("read_info_data is 0xe0,mistake \n");
			return 0;
		}
	switch (read_info_data[2])
	{
					case	ID_POWER:						
						break;
						
					case	ID_DISPLAY_SCREEN:
						///////////////for test,because this main board send info error. becarefull!
							switch (read_info_data[3])
								{
									case 	ID_SCREEN_MENU:
											MenuMainInit();
										break;		
									case 	ID_SCREEN_RADIO:
											MenuRadioInit();
										break;									
									case	ID_SCREEN_CDLOAD:											
									case	ID_SCREEN_CDDISP:
									case	ID_SCREEN_MP3DISP:
									case	ID_SCREEN_NODISC:
									case	ID_SCREEN_DISCERR:
									case	ID_SCREEN_EJECT:
											MenuCdInit();
											rece_handle = 0;
										break;
									case	ID_SCREEN_MEDIEA:									
										break;
									
									case 	ID_SCREEN_NAVI:
										MenuNaviInit();
										break;	
										
									case	 	ID_SCREEN_AUDIODISP:
											MenuAudioInit();
											break;	
									case		ID_SCREEN_VIDEODISP:
										MenuVideoInit();
										break;	
									case		ID_SCREEN_EQDISP:
											AudioEQInit();
										break;	
									case	 	ID_SCREEN_BASTRA:
											AudioBASInit();
										break;	
									case		ID_SCREEN_FADBAL:	
											AudioFADInit();							
										break;	
									}

									rc = 0;//if handled, clear this flag.	
								read_info_data[0] = 0;			
																									
						break;
						
					case	ID_KEY_STATUS:
						//it is identify the exit button.	
						
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;						
						break;
						
					case	ID_SCREEN_INFO:								
								
								if (read_info_data[3] == ID_SCREEN_INFO_VOLUME)
								{
									MenuSetDrawVolume(MenuSetFrame.pCanvas,1);	
								}
								break;
						
					case	ID_KEYPAD:	
							switch (read_info_data[3])
							{
									case 	ID_MUTE:
										MenuSetDrawVolume(MenuSetFrame.pCanvas,1);		
										break;
									case	ID_DMUTE:	
										MenuSetDrawVolume(MenuSetFrame.pCanvas,1);		
										break;									
									case 	ID_SEEKUP:
									case 	ID_SEEKDN:
									case 	ID_FMAM:
										MenuRadioInit();//band command;
										break;	
									
									case 	ID_DEST:
									case 	ID_NAVI:
										MenuNaviInit();
										break;
									
									case 	ID_CD:
										MenuCdInit();
										break;				
									case 	ID_MEDIA:
										MenuMediaInit();
										break;
									case 	ID_MENU:
										MenuMainInit();
										break;
										
									case	ID_TVAUX:
										
										break;
							}	
							rc = 0;//if handled, clear this flag.	
							read_info_data[0] = 0;						 
						break;
						
					case 	ID_BAND_SEL:
						MenuRadioInit();
						rc = 0;//if handled, clear this flag.	
						read_info_data[0] = 0;			
						break;		
						
					default:	
							rc = -1;						
						}
	return 0;					
}

void MenuSetInput(UInt32 key)
{
		int flag;
	
		if ( key == IRC_READ_INFO)
		{
			flag =message_dispatch ();
			if (flag == -1)
				printf ("main menu handle receive data error!\n");
				
		}
		
	if( key == IRC_TOUCH_DOWN || key == IRC_TOUCH_UP )
	{
		int focus;
		TPosCoor pos;
		dt_ClipTouchGetXY( &pos.x, &pos.y );
		printf ("in MenuSetInput key isIRC_TOUCH_DOWN IRC_TOUCH_UP \n");
		if( key == IRC_TOUCH_DOWN )
		{
			if( LastKey ==IRC_TOUCH_UP )
			{
				if( ( MenuSetFocus = MenuSetGetTouchID( pos )) != -1 )
				{
					MenuSetDrawItem( MenuSetFrame.pCanvas, MenuSetFocus, TRUE );
					//dtpresent_flag++;		
					//pthread_cond_signal(&dt_cond);
					//dt_Present();
					dt_Present_part(MenuSetTouchRect [MenuSetFocus]);
				}
			}
		}
		else
		{
			if( ( focus = MenuSetGetTouchID( pos ) ) == MenuSetFocus )
			{
				MenuSetBtnEnter( MenuSetFocus );
				LOG("Focus\n");
			}else if( MenuSetFocus != -1 )
			{
				MenuSetDrawItem( MenuSetFrame.pCanvas, MenuSetFocus, FALSE );
				//dtpresent_flag++;		
				//pthread_cond_signal(&dt_cond);
				//dt_Present();
				dt_Present_part(MenuSetTouchRect [MenuSetFocus]);
			}
			MenuSetFocus = -1;
		}
		LastKey = key;//hangsheng add 071126
	}
	if(mute_status ==2) 
	VolumeCountHandle ();
}

